package com.pateo.springcloudgoods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pateo.springcloudgoods.dao.mapper.auto.AutoGoodsMapper;
import com.pateo.springcloudgoods.dao.model.auto.AutoGoods;
import com.pateo.springcloudgoods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private AutoGoodsMapper goodsMapper;

    @Override
    public int insertGoods(AutoGoods goods) {
        goods.setId(null);
        return goodsMapper.insertSelective(goods);
    }

    @Override
    public int deleteGoods(Long id) {
        return goodsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateGoods(AutoGoods goods) {
        if(goods == null || goods.getId() == null){
            throw new IllegalArgumentException("参数错误");
        }
        return goodsMapper.updateByPrimaryKey(goods);
    }

    @Override
    public List<AutoGoods> selectAll(){
        return goodsMapper.selectByExample(null);
    }

    public PageInfo<AutoGoods> selectPage(Integer pageNum, Integer pageSize ){
        PageHelper.startPage(pageNum, pageSize); // 设定当前页码，以及当前页显示的条数
        List<AutoGoods> list = goodsMapper.selectByExample(null);
        PageInfo<AutoGoods> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public AutoGoods selectGoodsById(Long id) {
        AutoGoods goods = goodsMapper.selectByPrimaryKey(id);
        return goods;
    }
}
